﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.IO;
using System.Reflection;

namespace MyWindowsMediaPlayer_V2
{
    public class Bibliotheque : CollectionManager
    {
        public ObservableCollection<Media> tmp;
        private string imagePath;
        private string videoPath;
        private string musicPath;

        public Bibliotheque(System.Windows.Controls.MediaElement mediaElement)
        {
            mediaInstance = mediaElement;
            tmp = new ObservableCollection<Media>();
            loadBibliothequeFromPath();
            copyMediaListToTmp_List();
        }
        private void copyMediaListToTmp_List()
        {
            foreach (Media media in mediaList)
                tmp.Add(media);
        }
        private void resetMedia_List(ObservableCollection<Media> from)
        {
            if (mediaList.Count > 0)
                mediaList.Clear();
            foreach (Media media in from)
                mediaList.Add(media);
        }
        public void update()
        {
            removeAll_Items();
            loadBibliothequeFromPath();
        }
        public void orderBy_Musique()
        {
            ObservableCollection<Media> newOC = new ObservableCollection<Media>();

            if (mediaList.Count != tmp.Count)
            {
                mediaList.Clear();
                addMediaTo_List(tmp);
            }
            foreach (var media in mediaList.Where(v => v is Music))
                newOC.Add(media);
            resetMedia_List(newOC);
        }
        public void orderBy_Video()
        {
            ObservableCollection<Media> newOC = new ObservableCollection<Media>();

            if (mediaList.Count != tmp.Count)
            {
                mediaList.Clear();
                addMediaTo_List(tmp);
            }
            foreach (var media in mediaList.Where(v => v is Video))
                newOC.Add(media);
            resetMedia_List(newOC);
        }
        public void orderBy_Pics()
        {
            ObservableCollection<Media> newOC = new ObservableCollection<Media>();

            if (mediaList.Count != tmp.Count)
            {
                mediaList.Clear();
                addMediaTo_List(tmp);
            }
            foreach (var media in mediaList.Where(v => v is Pics))
                newOC.Add(media);
            resetMedia_List(newOC);
        }
        public void orderBy_AscendingName()
        {
            ObservableCollection<Media> newOC;

            if (tmp.Count > 0)
            {
                newOC = new ObservableCollection<Media>(mediaList.OrderBy(v => v.name));
                resetMedia_List(newOC);
            }
        }
        public void orderBy_DescendingName()
        {
            ObservableCollection<Media> newOC;

            if (tmp.Count > 0)
            {
                newOC = new ObservableCollection<Media>(mediaList.OrderByDescending(v => v.name));
                resetMedia_List(newOC);
            }
        }
        public void orderBy_AscendingArtist()
        {
            ObservableCollection<Media> newOC;

            if (tmp.Count > 0)
            {
                newOC = new ObservableCollection<Media>(mediaList.OrderBy(v => v.artiste));
                resetMedia_List(newOC);
            }
        }
        public void orderBy_DescendingArtist()
        {
            ObservableCollection<Media> newOC;

            if (tmp.Count > 0)
            {
                newOC = new ObservableCollection<Media>(mediaList.OrderByDescending(v => v.artiste));
                resetMedia_List(newOC);
            }
        }
        public void orderBy_AscendingDuration()
        {
            ObservableCollection<Media> newOC;

            if (tmp.Count > 0)
            {
                newOC = new ObservableCollection<Media>(mediaList.OrderBy(v => v.media.duration));
                resetMedia_List(newOC);
            }
        }
        public void orderBy_DescendingDuration()
        {
            ObservableCollection<Media> newOC;

            if (tmp.Count > 0)
            {
                newOC = new ObservableCollection<Media>(mediaList.OrderByDescending(v => v.media.duration));
                resetMedia_List(newOC);
            }
        }
        public void orderBy_AscendingType()
        {
            ObservableCollection<Media> newOC;

            if (tmp.Count > 0)
            {
                newOC = new ObservableCollection<Media>(mediaList.OrderBy(v => v.type));
                resetMedia_List(newOC);
            }
        }
        public void orderBy_DescendingType()
        {
            ObservableCollection<Media> newOC;

            if (tmp.Count > 0)
            {
                newOC = new ObservableCollection<Media>(mediaList.OrderByDescending(v => v.type));
                resetMedia_List(newOC);
            }
        }
        public void loadBibliothequeFromPath()
        {
            try
            {
                musicPath = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
                addMediaTo_List(Directory.GetFiles(musicPath));
            }
            catch (Exception)
            {
                musicPath = Directory.GetCurrentDirectory() + "\\Music";

                if (!Directory.Exists(musicPath))
                    Directory.CreateDirectory(musicPath);
            }
            try
            {
                imagePath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
                addMediaTo_List(Directory.GetFiles(imagePath));
            }
            catch (Exception)
            {
                imagePath = Directory.GetCurrentDirectory() + "\\Images";

                if (!Directory.Exists(imagePath))
                    Directory.CreateDirectory(imagePath);
            }
            try
            {
                videoPath = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);
                addMediaTo_List(Directory.GetFiles(videoPath));
            }
            catch (Exception)
            {
               videoPath = Directory.GetCurrentDirectory() + "\\Video";

               if (!Directory.Exists(videoPath))
                    Directory.CreateDirectory(videoPath);
            }
        }
    }
}